# -*- coding:utf-8 -*-

'''
PROJECT_NAME : auth_demo
file    : app3
author  : 1032162439@qq.com
date    : 2022-09-08 22:11
IDE     : PyCharm
'''
import json

from flask import Flask, request, make_response

app = Flask(__name__)

app.secret_key = 'secret key string'

token_dict = {
    'user_1': {
        'name': 'zhangsan',
        'pwd': '123456',
        'token': 'zhangsan token',
        'role': 'admin'
    },
    'user_2': {
        'name': 'lisi',
        'pwd': '123456',
        'token': 'lisi token',
        'role': 'user'
    }
}


@app.route('/gen_token/<user>')
def gen_token(user):
    resp = make_response('gen token success')
    resp.set_cookie('token', json.dumps(token_dict.get(user)))
    return resp


@app.route('/source')
def source():
    origin_token = request.args.get('token')
    if not origin_token:
        return '请传递token'
    token_dict = json.loads(origin_token)
    print(token_dict)
    if token_dict.get('role') == 'admin':
        return '管理员权限'
    elif token_dict.get('role') == 'user':
        return '普通用户权限'
    else:
        return '无权限'


if __name__ == '__main__':
    app.run(debug=True)
